package com.redwine.app.utils;

import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.redwine.app.APP;
import com.redwine.app.R;

/**
 * Created by gassion on 17/3/9.
 */
public class BitmapUtils {

    public static final int NORMAL = 1;
    /**
     * 高斯模糊
     */
    public static final int GAOSI = 2;


    /**
     * 剪切头像的圆形
     */
    public static final int CIRCLE = 4;

    /**
     * 圆角
     */
    public static final int ROUND = 5;

    /**
     * banner图
     */
    public static final int BANNER = 6;

    /**
     * 广告
     */
    public static final int ADVERTISING = 7;

    /**
     * 文章
     */
    public static final int THE_ARTICLE = 8;

    /**
     * 用户精品
     */
    public static final int USERS_BOUTIQUE = 9;

    /**
     * 封装此方法，便于修改图片网络框架
     *
     * @param img
     * @param url
     */
    public static void loadImage(ImageView img, String url) {

        loadImage(img, url, NORMAL);
    }

    public static void loadImage(ImageView img, int res) {
        picassoLoaderRes(img, res);
    }

    public static void loadImageRound(ImageView img, int res) {
        Glide.with(APP.getInstance())
                .load(res)
                .transform(new CenterCrop(APP.getInstance()), new GlideRoundTransform(APP.getInstance()))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade();

    }

    public static void loadImage(ImageView img, String url, int status) {
        switch (status) {
            case NORMAL:
                picassoLoader(img, url);
                break;
            case GAOSI:
                picassoLoader2Gaosi(img, url);
                break;
            case CIRCLE:
                picassoLoader2Circle(img, url);
                break;

            case ROUND:
                picassoLoaderRound(img, url);
                break;

            case BANNER:
                picassoBannerLoader(img, url);
                break;

            case ADVERTISING:
                picassoAdvertisingLoader(img, url);
                break;

            case THE_ARTICLE:
                picassoTheArticleLoader(img, url);
                break;

            case USERS_BOUTIQUE:
                picassoUsersBoutiqueLoader(img, url);
                break;
        }

    }

    private static void picassoLoaderRes(ImageView img, int res) {
        Glide.with(APP.getInstance()).load(res).diskCacheStrategy(DiskCacheStrategy.ALL).into(img);
    }

    private static void picassoLoader(ImageView img, String url) {
        Glide.with(APP.getInstance()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().into(img);
    }

    private static void picassoBannerLoader(ImageView img, String url) {
        Glide.with(APP.getInstance()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().placeholder(R.drawable.photo_normal_bg).error(R.drawable.photo_normal_bg).into(img);
    }

    private static void picassoAdvertisingLoader(ImageView img, String url) {
        Glide.with(APP.getInstance()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().placeholder(R.drawable.photo_normal_bg).error(R.drawable.photo_normal_bg).into(img);
    }

    private static void picassoTheArticleLoader(ImageView img, String url) {
        Glide.with(APP.getInstance()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().placeholder(R.drawable.photo_normal_bg).error(R.drawable.photo_normal_bg).into(img);
    }

    private static void picassoUsersBoutiqueLoader(ImageView img, String url) {
        Glide.with(APP.getInstance()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().placeholder(R.drawable.photo_normal_bg).error(R.drawable.photo_normal_bg).into(img);
    }

    private static void picassoLoaderFit(ImageView img, String url) {
        Glide.with(APP.getInstance()).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().into(img);
    }

    private static void picassoLoader2Circle(ImageView img, String url) {
        Glide.with(APP.getInstance())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new CircleTransformation(APP.getInstance()))
                .placeholder(R.drawable.icon_default_head)
                .error(R.drawable.icon_default_head)
                .into(img);
    }

    private static void picassoLoaderRound(ImageView img, String url) {
        Glide.with(APP.getInstance())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.photo_normal_bg)
                .error(R.drawable.photo_normal_bg)
                .transform(new CenterCrop(APP.getInstance()), new GlideRoundTransform(APP.getInstance()))
                .into(img);
    }

    private static void picassoLoader2Gaosi(ImageView img, String url) {
        Glide.with(APP.getInstance())
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .placeholder(R.drawable.photo_normal_bg)
                .error(R.drawable.photo_normal_bg)
                .crossFade(1000)
                .bitmapTransform(new GaosiTransformation(APP.getInstance(), 10, 1))
                .into(img);
    }


}
